home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 05 Programming / INTRO.4TH < prev    next >
Text File  |  2019-04-13  |  21KB  |  522 lines

  1.         INTRO.4TH      (Advantages of Forth)
  2.  
  3.     Preface:
  4.  
  5. The purpose of this file is not just to tell you "Forth is Great", but rather
  6. to clarify just what advantages Forth has over other languages, and to
  7. communicate the "feel" of Forth in relation to programming tasks.
  8.  
  9. I hope it does that job well enough to give you a sense of the possibilities,
  10. so that, when you pursue your desire to see for yourself what you can
  11. accomplish with it, you will be expecting to create elegance.  And if you are
  12. intent, you will be gratified.
  13.  
  14. Please read this without bias, and if you doubt any statements made, the author
  15. is prepared to provide proof or acceptable evidence, or upload a corrected
  16. version of this file.
  17.  
  18. If, on the other hand, you are not willing to find out that there is a
  19. programming language considerably more capable and elegant than others you are
  20. familiar with, then better to not even give it a single quick scan!  (grin)
  21.  
  22.                                                     Giles
  23.  
  24.  
  25.     Topic Headings
  26.  
  27. Abreviation Definitions
  28. Commands That Fit
  29. Programming Environment
  30. Personal Experience
  31. What is Forth?
  32. Different Installations
  33. Examples
  34. Small Concept= Large Capability
  35. Grand Scale Programming
  36. Use
  37. My plans
  38. Your application
  39. Availability on CIS
  40. Learning Forth
  41. Social Aspects
  42. Problems
  43.  
  44.  
  45.     Abreviation Definitions
  46.  
  47. hdw., Hardware
  48. OS, Operating System
  49. PD, Public Domain
  50. sfw., software
  51.  
  52.  
  53.     Commands That Fit
  54.  
  55.   Have you ever wished:
  56.  
  57. 1. A particular command went about things just a little differently?
  58.  
  59. 2. A particular command behaved consistantly, rather than doing the wrong thing
  60.    at just the wrong time?
  61.  
  62. 3. You had a particular command, that would make things SOOooooo simple?
  63.  
  64. In Forth, the process of finding the code of a command and installing a
  65. rewritten version is trivial.
  66.  
  67.  
  68.     Programming Environment
  69.  
  70. Have you ever felt that programming would always be a pleasant experience, if
  71. only You never had to program around bad system software?
  72.  
  73.   Have you ever wished:
  74.  
  75. 1. You could program twice as fast?  ..Five times as fast?  ..TEN times as
  76.    fast?!  And that you could debug the program as you write it, so that
  77.    (generally) when you finished writing a module you wouldn't have to come
  78.    back to it?
  79.    (Forth often takes on the order of a hundredth the time equivalent programs
  80.    can be coded in assembler. It also happens often enough, that assembler
  81.    takes MORE than 100 times as long and the program STILL won't work
  82.    satisfactorily. (grin)
  83.  
  84. 2. That all the clever little subroutines you've worked out were instantly
  85.    available, without having to look for them, or take any special pains to
  86.    integrate them into the present program.
  87.  
  88. 3. That calling a subroutine required nothing more than naming it?
  89.  
  90. 4. That someway your programming environment would help you keep track of where
  91.    you are, and how the routine you are working on now will affect other
  92.    routines, or which ones will be affected?
  93.  
  94. 5. That you could change the way your compiler behaves?  Or that you could add
  95.    the ability to handle special cases without changing anything else?
  96.  
  97. 6. That you could use hi-level commands, and a magic genie would come along and
  98.    convert them to efficient machine code?  Or maybe just the ones needing
  99.    speed, and leaving the others in the most compact form to save memory?
  100.  
  101. 7. That you never had to worry about linking library routines, or that ONE copy
  102.    of routines would suffice for ALL processes?
  103.  
  104. 8. That you could have a multitasking, multiuser operating system with all the
  105.    features of, say for instance, OS9 or Unix, but ALSO have the immediate
  106.    utility of a powerful toolbox and programming language combined?
  107.  
  108. 9. That you could bring the same, familiar environment and applications from
  109.    obsolete hardware to new hardware with expanded capabilites, and easily add
  110.    features to take advantage of the new hardware without having to change much
  111.    if any of what you already have?  Examples:  Transfer your system and
  112.    applications to a Tandy Color Computer in order to have graphics that beat
  113.    some MS-DOS and Macintosh machines at a fraction of the price.  Or to an IBM
  114.    styled machine, in order to take advantage of all the hardware options
  115.    available, yet having an EFFECTIVE multasker, instead of being stuck with
  116.    OS/2.
  117.  
  118. 10.  That you had a powerful environment on which you could write a similar
  119.    environment, (but with all your own improvements, of course), and compile
  120.    the new environment and have a system of your very own, with no royalties
  121.    owed to anyone?  (Perhaps for your own system to be manufactured and sold.)
  122.  
  123. 11.  To have the ability to implement other languages, such as BASIC, LISP,
  124.    Pascal, Prolog simply and efficiently?
  125.  
  126. In Forth, you can move about freely.  There is no "black box" compiler to
  127. shackle you with unnecessary restraints.  No restriction to predefined data
  128. structures.  You cannot get lost in GOTOs or jumps because Forth neither has
  129. nor needs them.
  130.  
  131. Forth assumes you know what you are doing, and often how you do it is implicit.
  132.  
  133.  
  134.     Personal Experience
  135.  
  136. Before I knew of the existance of Forth I knew I would have to have a language
  137. that was infinitely extensible, and selfcompiling to do the things I wanted to
  138. do.  When I learned what Forth was, it was like manna from heaven.
  139.  
  140. With systems such as OS9 having some of the advantages of Forth, and excellent
  141. file handling, and with the availability of a powerful text editor such as
  142. TSEdit, a person has a very effective system for gathering textual data
  143. piecemeal and editing it.
  144.  
  145. But in using such a system, I have had to program around deficiencies of other
  146. languages, been so hampered in reaching the hardware functions of the machine,
  147. that some applications that should have been done were not practical to tackle,
  148. (because of time restraints) and as a result been forced to use software for
  149. which we haven't the source.  And so have been unable to adapt it exactly to
  150. our needs, or add enhancements and upgrades that we should have had.
  151.  
  152. So, I am FORCED to incorporate the advantages of OS9 into Forth, as I can no
  153. longer do without Forth's other, inherent advantages.
  154.  
  155. For a somewhat different experience, you might want to read Doug Hoffman's file
  156. "NOVICE.TXT" in DL 1 at ForthSIG. (g forth.)   It's ASCII and you can REAd it,
  157. though ACCESS thinks it's binary.
  158.  
  159.  
  160.     What is Forth?
  161.  
  162. Forth is an infinitely extensible metacompilable language/operating system in
  163. which subroutining is inherent.  (Metacompilable is a Forth term meaning it can
  164. compile new versions of itself from source)
  165.  
  166. The stack's use is direct and largely automatic, and you can directly
  167. manipulate it.
  168.  
  169. Though most implementations are sparse from an applications viewpoint, (though
  170. I know of none that do not include an editor and assembler), it is so trivial
  171. to extend it, that you can relatively easily write a LISP or BASIC interpreter,
  172. C compiler, or a Prolog extension.  (I have seen most of these in sigs or
  173. elsewhere.)
  174.  
  175. Since Forth is so compact, (it compiles to only subroutine addresses), and so
  176. fast, (though slower than Machine Language to do exactly the same operations,
  177. practically it's often faster, because of the better organization of the
  178. functional blocks), and it can duplicate the power of any existing language, as
  179. well as any you dream up that are MORE powerful, it's hard to deny that Forth
  180. can be the MOST powerful language there is.  As a practical matter, complete
  181. control is in the programmer's hands, so the extensions will be just as bad as
  182. the programmer forces them to be!  (grin)
  183.  
  184. On the other hand, you don't have to say "stupid compiler writer!  Why didn't
  185. he do it THIS way?!".  You can just recompile the whole system to suit
  186. yourself!  Good idea to, because that way, it is your own system. to sell as an
  187. application, developmment system, arcade game, or whatever, with no royalties
  188. owed to anyone, and as a complete standalone system.
  189.  
  190.  
  191.     Different Installations
  192.  
  193. It is also easily transportable.  You can redefine the primitives for the new
  194. cpu, and recompile it, with all the resources of the present system at hand.
  195.  
  196. And because of the small number of necessary primitives, and of the extreme
  197. modularity and interactive method of programming this allows, you will not
  198. believe how easy such a port is until you see it!
  199.  
  200.  
  201.     Examples
  202.  
  203. You may know that the RSdos BASIC PALETTE command (for the Coco) takes a slot
  204. number (0-15) and puts a color value (0-63) into it.  The zero slot's hardware
  205. address is 65456.  So I defined a PALETTE command in Forth thusly:
  206.  
  207.   : PAL -80 + C! ;
  208.  
  209. :  is a word called Colon, and is the most commonly used compiler.  It enters
  210. the following word (a word is a group of non-space characters bounded by
  211. spaces, in this case PAL), into the dictionary, and compiles the addresses of
  212. the following words, (which normally must have already been defined), into the
  213. dictionary as its definition.
  214.  
  215. -80 is the signed 16 bit integer equivalent of 65456.  Mention of a number puts
  216. it on the stack.
  217.  
  218. + takes the 2 top stack entries and adds them, leaving the result on top of the
  219. stack.
  220.  
  221. ! is pronounced "store".  It takes the second stack item and stores it in the
  222. address on top of the stack.  But it stores SIXTEEN bit values.  In this case
  223. we only want to store 1 byte, so we use Cstore, C being for "character".  Of
  224. course C! is a different word from !.
  225.  
  226. ; terminates all definitions started with :.
  227.  
  228. So now instead of PALETTE8,38 as I would use in basic, I can do 38 8 PAL.
  229.  
  230. 38 pushes 38 on the stack.
  231.  
  232. 8 pushes on the 8.
  233.  
  234. PAL first takes the top item and adds it and 65456 to give the address of slot
  235. 8, then stores 38 (second stack item) into one byte at that address.  And all
  236. of a sudden, your text is orange.  (grin)
  237.  
  238. To make this command "system independent", you could first define a constant,
  239. SL0T, thusly:
  240.  
  241.  65456 CONSTANT SL0T
  242.  
  243. (Constant is a compiling word that compiles...You got it, Constants!  (grin))
  244.  
  245.   Then define PAL thusly:
  246.  
  247.     : PAL SL0T + C! ;
  248.  
  249. That way, only SL0T would have to be changed for another system.  All system
  250. dependent values could be stored in a single place, and so be easy to set up
  251. for a different system.
  252.  
  253. Here's an edited example of the procedure for installing a rewritten primitive,
  254. as explained by my friend Bob McIsaac:
  255. --------------
  256.  ' <EMIT> DUMP ..display code being used.
  257.   You can figure a new version then install the hex code
  258. using:
  259.  CREATE EMIT NNNN , NNNN , NNNN , NEXT SMUDGE
  260.   If it works then install it:-
  261.  ' EMIT ' <EMIT> CFA ! FREEZE
  262.   This works because the code field address of a word always
  263. points to executable code and this can be anywhere in
  264. memory.
  265. ---------------
  266.  
  267. In this example:
  268.  
  269. A dictionary header is made up like this:  Up to the first 31 bytes are the
  270. NAME FIELD.  Next 2 bytes are the LINK FIELD, which points to the previously
  271. defined word.
  272.  
  273. Next 2 bytes are the CODE FIELD, and point to executable code.  (The following
  274. parameter field, if the word is a primitive.)  The remaining bytes are the
  275. PARAMETER FIELD, which may contain executable code, addresses of other words,
  276. variable storage, constant storage, tables, or what have you.
  277.  
  278. ' (pronounced "tick") puts the PFA (Parameter Field Address) of the word
  279. following it, on the stack.
  280.  
  281. DUMP gives a hex dump starting at the address on the stack.
  282.  
  283. CREATE creates a dictionary header for the word following it, in which the 1st
  284. character is altered (smudged, by flipping a bit) so that it can't be found,
  285. (while it is being defined).
  286.  
  287. , (Comma) Inserts the 16 bit value on top the stack at the current end of the
  288. dictionary, and bumps the dictionary pointer 16 bits, (to the NEW end of
  289. dictionary).
  290.  
  291. NEXT puts code in place for a "return" for the virtual Forth machine.
  292.  
  293. SMUDGE flips the flipped bit in the altered letter of the keyword, so that
  294. actually, in this case, it's working more like an "unsmudge".  (grin)
  295.  
  296. CFA takes a PFA and converts it to a CFA (Code Field Address).  In this case,
  297. the PFA of <EMIT> is converted to its CFA
  298.  
  299. !  Takes the second stack item (in this case, the PFA of EMIT) and stores it in
  300. 16 bits at the address on top of the stack, (in this case, the CFA of <EMIT>).
  301.  
  302. FREEZE sets the system so that it permanently recognizes this installation.
  303.  
  304.  
  305.     Small Concept= Large Capability
  306.  
  307. Just a few additional concepts give you a great deal of added power.
  308. Understanding "Compile time" and "Run time" behaviors of words, and "Compiling"
  309. words, allows you to do things in the most direct way possible.
  310.  
  311. The "Reverse Polish Notation" use of the stack allows elimination of nearly all
  312. other local variables.
  313.  
  314. I find Forth the easiest language in which to conceptualize a task.  Many
  315. procedures that would not be trivial in any OTHER design of language, can be
  316. programed in Forth out of hand.  Each part of the problem can always be
  317. programmed seperately, as a preliminary word to the final word that does the
  318. whole job.  Also you can start out with a word that just handles limited
  319. conditions, and keep adding new functions to it as you elaborate on the
  320. problem.
  321.  
  322. If you choose word names carefully, Forth is self documenting; also you can add
  323. comments inside (or outside, of course) the definition.
  324.  
  325.  
  326.     Grand Scale Programming
  327.  
  328. I have talked about designing or redesigning whole systems, for customization,
  329. or porting to new hardware, and I expect it sounds like something that someone
  330. might spend months, or maybe even years doing, but it more likely would take
  331. you only a matter of days, because:
  332.  
  333. 1. Forth systems tend to be very small.
  334.  
  335. 2. You usually have the tools to do things in the most direct way possible.
  336.    When you don't, you can MAKE them.
  337.  
  338. 3. The combination of interactive programming with very small modules which are
  339.    all subroutines usable individually, allows you to very efficiently
  340.    "snowball" the power of your code.  In other words, though in any language a
  341.    good programmer tries to write code whose power increases geometrically with
  342.    the amount of code he writes, Forth was designed to allow you to do this
  343.    very efficiently.
  344.  
  345. 4. The modularity, interactivity, use of subroutines by naming them, as well as
  346.    automatic stack use, makes debugging a breeze!
  347.  
  348.  
  349.     Use
  350.  
  351. You can code interactively, at the terminal, to test things, but then for real
  352. work, you enter the editor, and edit scripts or source onto disk.
  353.  
  354. Then you can take input from disk with a LOAD command, and do execution and
  355. compiling directly from disk (until your file gives the command to take input
  356. from terminal).
  357.  
  358. Forth can accomplish everything OS9 or Unix can, more simply.
  359.  
  360.  
  361.     My plans
  362.  
  363. I've always wanted to write my own OS because I eventually wanted to put out my
  364. own hardware, and wanted complete control of maintenance and no royalties.
  365. There are many PD Forths, and Forth workalikes out there to build upon.
  366.  
  367. The system I want to develop now would include "Universal Random Block Access",
  368. or URBA which means the ability to access all common disk formats, (Mac, MSdos,
  369. Amiga, OS9, Unix, RSdos, CPM, ProDOS, CDOS, etc.)  Each format description
  370. would be contained in a "DOS Descriptor Block" data structure, and if a new
  371. format became popular, you could just add another DDB.  With additional device
  372. descriptors, and drivers, you could access WORMs, and VHS, etc.
  373.  
  374. Naturally it will have multitasking, and I want to add all the most useful
  375. commands of OS9 and Unix.  Of course it will always have the advantage of being
  376. a programming language as well as an OS, over OS9 and Unix.
  377.  
  378.  
  379.     Your Application
  380.  
  381. Although there are versions that run on top of OS9, Flex, CPM, MSdos,
  382. etc., etc, to get the greatest advantages, you need Forth on the bottom!
  383.  
  384.  
  385.     Availability on CIS
  386.  
  387. Put your checkbook away!  You can get versions of Forth for the popular CPUs
  388. for down-load time:
  389.  
  390.     6809
  391.  
  392. The TIL09 system is on CocoSIG (go coco), in "Languages and Op.  Systems" Lib.
  393. It makes ROM calls on the Tandy Color Computer.
  394.  
  395.   Here is a list of necessary files:
  396.     TIL09.DOC   and
  397.     M4TH.DOC    Describe operation and capabilities of TIL09.
  398.     TEDIT.DOC   Describes editor use, and other necessary commands
  399.                    imbedded in the editor.
  400.     TILBUG.4TH  Bug fixes.
  401.     TIL09.GLO   System glossary.
  402.     TILASM.DOC  Overview of assembler.
  403.     TIL09.BIN   Executable system.  From BASIC, do   LOADM"TIL09":EXEC.
  404.                   Uses logical sector/block disk access.
  405.     TILDOS.ARC  TILDOS uses RSDOS disk files. Instructions are
  406.                    included.
  407.     META1.BIN   and
  408.     META2.BIN   are source for TIL09.
  409.     TDCMP.BIN   is source for the metacompiler/decompiler.
  410.     TEDIT.BIN   is the source for the editor.
  411.     TILASM.4TH  source for the assembler.
  412.  
  413.     68000
  414.  
  415. Do a go clm332 to get to Computer Languages Magazine's forum, and there, in the
  416. Forth library you can find:
  417.  
  418.     README.68K  installation instructions.
  419.     F83-68.BIN  the executable Forth system. (Uses CPM disk access.)
  420.     KER68K.SCR  source, necessary for adapting the system and
  421.                 helpful in understanding it.
  422.     META68.SCR  source for the metacompiler, n case you want to alter the
  423.                 system, or make a completely new one.
  424.     UTL68K.SCR  source for utilities.
  425.  
  426. Ataripro forum has basically the same forth as F83-68K, but different disk
  427. access extensions for a non CP/M Forth.
  428.     F83.TOS     The Atari ST version of Forth-83.  Includes some Atari xbios
  429.                 functions.
  430.     KERNEL.BLK  Kernel source.
  431.     KERNEL.TOS  The Forth kernel used for
  432.                 extending F83.
  433.     META.BLK    The F83 meta-compiler.
  434.     TRAPS.BLK   Atari xbios functions
  435.     F83KER.ARC  and
  436.     F83BLK.ARC  Sources for full blown Forth'83 and metacompiling for the Atari
  437.                 ST.
  438.     CPU68.BLK   Assembler and low-level routines used by the debugger and
  439.                 multi-tasker.
  440.     EXTEND.BLK  Extensions source.
  441.  
  442.     8086
  443.  
  444.   Also in CLM's Forth Library:
  445.  
  446.     README.PC   Dox for MS-DOS Forth.
  447.     CMD4TH.BIN  IBM-PC dedicated Forth. (Does NOT go thru PC-DOS)
  448.     F83-86.BIN  DOES go thru PC/MS-DOS.
  449.     KER-86.SCR  System source.
  450.     CPU-86.SCR  Assembler source.
  451.     META86.SCR  Metacompiler source.
  452.  
  453.     8080
  454.  
  455.   Also in CLM's Forth Library:
  456.  
  457.     README.80    Documentation
  458.     KERNAL.SCR   System source. (CPM80)
  459.     F83-80.BIN   Executable system.(CPM80)
  460.  
  461.  
  462.     Learning Forth
  463.  
  464.    "Starting Forth" by Leo Brodie: I'd recommend this for a first text.
  465.     TIL09.GLO: This glossary is in CocoSIGs DL9.  Maybe not Forth '83 de
  466.                rigueur, but it is helpful.
  467.    "The Forth '83 Standard": The entire text for this is available in the
  468.                              Forth forum, (g forth), in DL 2, as FORTH-.DOC.
  469.     NOVICE.TXT: Is in DL 1 in ForthSIG.  You might want to read this file for
  470.                 the experiences of a beginner, in hopes of avoiding his
  471.                 frustrations.   Dispite the warning that it is binary, you can
  472.                 do a REAd on it.
  473.     4THRES.TX0 (Forth resorce): This reference file is in Forth library in CLM
  474.                 (g clm332)....  You will find FIG's address in here.
  475.  
  476. We are planning a Forth workshop in CocoSIG.  Anyone interested may contact me
  477. for the schedule.
  478.  
  479. Also, if anyone knows of other live "Forth Introductions", I'll appreciate
  480. hearing of them.
  481.  
  482.  
  483.     Social Aspects
  484.  
  485. There are people, some of whom are pretty good programmers, that consider Forth
  486. a "Cult" language.  I've tried to figure why this is so, (*I* don't feel like a
  487. cultist.(grin)), and so far I've come up with these possibilities:
  488.  
  489. 1. Forth IS after all fundamentally "different" from any other language.
  490.    (Despite its similarity to LISP.)
  491.  
  492. 2. Once some people realize how much they can accomplish with Forth, they may
  493.    get overexcited, and sound as though they have gone overboard!
  494.    (Hehe..couldn't happen to ME!)
  495.  
  496. 3. I think it likely that Forth is better than what they (the people that call
  497.    it a "cult" language) are used to, and maybe, for whatever reason, they
  498.    would rather not find that out!  (grin)
  499.  
  500. 4. Though Forth tends to attract good programmers (The best!), it also
  501.    attracts a few poor ones.  (I once saw a guy use a whole screen to define a
  502.    single primitive (grin).)  Such a programmer is not likely to present
  503.    Forth to a newcomer in its best light.  Also if the newcomer is himself a
  504.    fairly good programmer, he's likely to figure that if his less capable
  505.    friend thinks well of a language, it's probably a dud!  <Hehe> So, then, if
  506.    he hears the enthusiasm of someone who really knows and uses the potential
  507.    of Forth somewhere near its fullest, he's likely to attribute such
  508.    enthusiasm to "cultism".  (grin).
  509.  
  510.  
  511.  
  512.     Problems
  513.  
  514.  Plex or contact me as follows:
  515.   Giles Spruill 73347,2651
  516.   Advanced Automation, Inc.
  517.   1217 N.E. Miami Ct., Suite 6
  518.   Miami, FL 33132
  519.   Ph# (305) 371-6408
  520.  
  521. I would also appreciate any advice that would help make this file better.
  522.